<?php
namespace app\models;

use Yii;
use app\models\Orderinfo;
use app\models\PlanBillItem;

class PlanBill
{
	public static function getPlannumQuery($orderunit,$receivingunit,$orderdate,$orderdate_end,$deliverydate,$deliverydate_end)
	{
		$query = new \yii\db\Query();

		$query->select("planbillnum as plannum")
			->from("JXC_PLANBILL")
			->andWhere("orderunit like '%$orderunit%'")
			->andWhere("receivingunit like '%$receivingunit%'");
		if ($orderdate != "") {
			$query->andWhere("orderdate >= '$orderdate'");
		}
		if ($orderdate_end != "") {
			$query->andWhere("orderdate <= '".$orderdate_end." 23:59:59'");
		}
		if ($deliverydate != "") {
			$query->andWhere("deliverydate >= '$deliverydate'");
		}
		if ($deliverydate_end != "") {
			$query->andWhere("orderdate <= '".$deliverydate_end." 23:59:59'");
		}

		return $query;
	}

	public static function getPlannum($orderunit,$receivingunit,$orderdate,$orderdate_end,$deliverydate,$deliverydate_end)
	{
		$query = new \yii\db\Query();

		$query->select("planbillnum as plannum")
			  ->from("JXC_PLANBILL")
			  ->andWhere("orderunit like '%$orderunit%'")
			  ->andWhere("receivingunit like '%$receivingunit%'");

		if ($orderdate != "") {
			$query->andWhere("orderdate >= '$orderdate'");
		}
		if ($orderdate_end != "") {
			$query->andWhere("orderdate <= '".$orderdate_end." 23:59:59'");
		}
		if ($deliverydate != "") {
			$query->andWhere("deliverydate >= '$deliverydate'");
		}
		if ($deliverydate_end != "") {
			$query->andWhere("orderdate <= '".$deliverydate_end." 23:59:59'");
		}

		$plannumList = $query->all();

		$result = array();

		foreach($plannumList as $plannum){
			$result[] = '\''.$plannum['plannum'].'\'';
		}

		return implode(",",$result);
	}

	public static function getIdByOrdernum($ordernum)
	{
		$orderinfoList = Orderinfo::getIdByOrdernum($ordernum);
		$planbillList = PlanBillItem::getPlanbillid($orderinfoList);

		return $planbillList;
	}

	public static function getPlanidByPlannum($plannum)
	{
		$query = new \yii\db\Query();
		
		$planidList = $query->select("planbillid")->from("JXC_PLANBILL")->where("planbillnum like '%".$plannum."%'")->all();

		$result = array();
		foreach($planidList as $planid){
			$result[] = $planid['planbillid'];
		}

		return implode(",",$result);
	}
}